package com.he.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.he.pojo.Permission;
import com.he.pojo.PermissionDetail;
import com.he.pojo.PermissionDetailExample;
import com.he.pojo.PermissionDetailExample.Criteria;
import com.he.pojo.Role;
import com.he.service.permission.PermissionServiceI;
import com.he.service.permissionDetail.PermissionDetailServiceI;
import com.he.service.role.RoleServiceI;

@Controller
@RequestMapping("rolepermiss")
public class RolePermissController {
	@Autowired
	PermissionDetailServiceI detailService;
	@Autowired 
	RoleServiceI roleService;
	@Autowired
	PermissionServiceI permissionService;
	@Transactional
	@RequestMapping("show")
	public String rolePermissShow(ModelMap map,HttpServletRequest request) {
		Role role2=new Role();
		String parameter = request.getParameter("pageNo");
		if(!(parameter==null)) {
			role2.setPageNo(Integer.valueOf(parameter));
		}
		List<Role> roles = roleService.getRoles(null, role2);
		for (Role role : roles) {//遍历角色表
			List<Permission> permissions=new ArrayList<>();
			PermissionDetailExample example=new PermissionDetailExample();//找到当前角色对应所有权限
			//在权限详情表中查找当前角色，有权限（isPermission为1的权限）
			example.createCriteria().andRoleIdEqualTo(role.getId()).andIsPermissionEqualTo((byte)1);
			PermissionDetail detail = new PermissionDetail();
			detail.setWithPage(0);
			List<PermissionDetail> permissionDetails = detailService.getPermissionDetails(example, detail);
			for (PermissionDetail permissionDetail : permissionDetails) {
				permissions.add(permissionService.getPermissionById(permissionDetail.getPermissionId()));
			}
			role.setPermissions(permissions);
		}
		Permission permission=new Permission();
		permission.setWithPage(0);
		List<Permission> permissions = permissionService.getPermissions(null, permission);
		map.addAttribute("permissions", permissions);
		map.addAttribute("roles", roles);
		/*为rolePermiss总页数赋值,向前端传总页数*/
		PageInfo<Role> info=new PageInfo<>(roles);
		//为rolePermiss总页数赋值
		role2.setPages(info.getPages());
		//为rolePermiss总记录数赋值
		role2.setTotal(info.getTotal());
		/*为rolePermiss返回的controller链接赋值*/
		role2.setUrl("show.do");
		/*向前端传参数保存在user中*/
		map.addAttribute("role2", role2);
		return "PerRoleDetail";
	}
	/**
	 * 封装判断详情表中职位是否含有权限，表中不存在则创建，存在则更新
	 * @param i 插入、更新操作的返回值
	 * @param permission2 
	 * @param permissionDetail
	 * @param b 存入的值是否有权限0代表没有权限，1代表有权限
	 * @return
	 */
	@Transactional
	public int ispermiss(int i,Permission permission2,PermissionDetail permissionDetail,byte b) {
		PermissionDetail detail2=new PermissionDetail();
		detail2.setWithPage(0);
		PermissionDetailExample example=new PermissionDetailExample();
		example.createCriteria().andRoleIdEqualTo(permissionDetail.getRoleId())
			.andPermissionIdEqualTo(permission2.getId());
		List<PermissionDetail> Details = detailService.getPermissionDetails(example, detail2);
		//赋值
		permissionDetail.setPermissionId(permission2.getId());
		permissionDetail.setIsPermission((byte)b);
		if(Details.isEmpty()) {//如果详情表中该角色不存在该权限，则插入
			i +=detailService.insert(permissionDetail);
		}else {//如果详情表中该角色存在该权限，则isPermission赋值为1
			 i += detailService.updateByExample(permissionDetail, example);
		}
		System.out.println("iiiiiiiiiiii"+i);
		return i;
	}
	@RequestMapping(value="update",method=RequestMethod.POST)
	@ResponseBody
	public String rolePermissUpdatePost(PermissionDetail permissionDetail,HttpServletRequest request) {
		int i=0;
		String[] parameterValues = request.getParameterValues("permissionId");//获取该职位所具有的所有权限id
		//遍历权限表
		Permission permission=new Permission();
		permission.setWithPage(0);
		List<Permission> permissions = permissionService.getPermissions(null, permission);
		//从前端传入参数为空，代表没有任何权限
		if(parameterValues==null) {
			for (Permission permission2 : permissions) {
				i=ispermiss(i, permission2, permissionDetail,(byte)0);
				}
		}
		//从前端传入参数总数等于权限表总记录数，代表有所有权限
		else if(permissions.size()==parameterValues.length) {
			for (Permission permission2 : permissions) {
				i=ispermiss(i, permission2, permissionDetail,(byte)1);
				}
		}else {
			for (Permission permission2 : permissions) {
				int flag=0;
				//遍历从前端获取到的权限id
				for (String s : parameterValues) {
					if(Integer.valueOf(s)==permission2.getId()) {//查看权限详情表中该角色是否包含此项权限，不存在则插入
						i=ispermiss(i, permission2, permissionDetail,(byte)1);
						break;
					}else {
						flag+=1;
					}
				}
				if(flag==parameterValues.length&&permissions.size()!=parameterValues.length) {
					i=ispermiss(i, permission2, permissionDetail,(byte)0);
				}
			}
		}
		System.out.println(i);
		if(i==permissions.size()) {
			return "1";
		}
		else {
			return "0";
		}
	}
}
